package com.chris.cc.app.service.wrapper;

import com.chris.cc.common.exception.InvalidArgumentException;
import com.chris.cc.app.model.dto.MenuDto;
import com.chris.cc.app.model.vo.MenuVO;
import com.chris.cc.app.service.MenuService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Copyright (c) 2018-2025, chris All rights reserved.
 * <p>
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * <p>
 * Redistributions of source code must retain the above copyright notice,
 * this tree of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this tree of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * <p>
 * Author: haoka (haokang207@126.com)
 * <p>
 * Date: 2018/8/27 14:11
 **/
@Service
public class MenuWrapper {

    @Autowired
    private MenuService menuService;

    public void showMenuOnAppUp(String appName) {
        menuService.showMenu(appName);
    }

    public void hideMenuOnAppDown(String appName) {
        menuService.hideMenu(appName);
    }

    public List<MenuVO> tree(Boolean visible) {
        return _tree(null, visible);
    }

    private List<MenuVO> _tree(MenuVO parent, Boolean visible) {
        Long parentId = 0L;
        if (parent != null) {
            parentId = parent.getId();
        }
        List<MenuVO> list = menuService.listMenu(parentId, visible).stream()
                .map(MenuVO::from)
                .collect(Collectors.toList());
        for (MenuVO p : list) {
            p.setParentId(parentId);
            p.setChildren(_tree(p, visible));
        }
        return list;
    }

    @Transactional
    public void add(MenuDto menu) throws InvalidArgumentException {
        if (StringUtils.isBlank(menu.getName())) {
            throw new InvalidArgumentException("名称不能为空", menu.getName());
        }
        if (menu.getParentId() == null) {
            throw new InvalidArgumentException("父菜单ID不能为空", menu.getParentId());
        }
        if (menu.getType() == null) {
            throw new InvalidArgumentException("菜单类型不能为空", menu.getType());
        }
        menuService.add(menu);
    }

    @Transactional
    public void update(MenuDto menu) throws InvalidArgumentException {
        if (menu.getId() == null) {
            throw new InvalidArgumentException("菜单ID不能为空", menu.getId());
        }
        if (StringUtils.isBlank(menu.getName())) {
            throw new InvalidArgumentException("名称不能为空", menu.getName());
        }
        if (menu.getParentId() == null) {
            throw new InvalidArgumentException("父菜单ID不能为空", menu.getParentId());
        }
        if (menu.getType() == null) {
            throw new InvalidArgumentException("菜单类型不能为空", menu.getType());
        }
        menuService.update(menu);
    }

    @Transactional
    public void delete(Long id) {
        if (id == null) {
            return;
        }
        menuService.delete(id);
    }

    public MenuVO get(Long id) {
        if (id == null) {
            return null;
        }
        MenuVO vo = MenuVO.from(menuService.get(id));
        vo.setChildren(_tree(vo, false));
        return vo;
    }
}
